package com.tangr.adapterapplication;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.tangr.normaladapter.recyclerview.MultiItemAdapter;
import com.tangr.normaladapter.recyclerview.RecyclerAdapter;


/**
 * Created by tangr on 2016/7/28.
 */
public class ContactRecyclerView extends FrameLayout {
    private RecyclerView mRecyclerView;
    private FrameLayout mFrameLayout;
    private RecyclerAdapter adapter;
    private TextView headerText;
    private boolean hasScroll;

    public ContactRecyclerView(Context context) {
        super(context);
        initView();
    }

    public ContactRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public ContactRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_contactrecyclerview,this,true);
        mRecyclerView = (RecyclerView) findViewById(R.id.pullrecyclerview);
        mFrameLayout = (FrameLayout) findViewById(R.id.header);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                hasScroll = true;
                View firstView = recyclerView.findChildViewUnder(mFrameLayout.getMeasuredWidth() / 2, 0);

                if (firstView != null && firstView.getContentDescription() != null) {
                    headerText.setText(firstView.getContentDescription());
                }

                View view = recyclerView.findChildViewUnder(mFrameLayout.getMeasuredWidth() / 2, mFrameLayout.getMeasuredHeight());
                if (view != null && view.getTag(R.id.tag_itemView) != null && ((int)view.getTag(R.id.tag_itemView)) == 1) {
                    if (view.getTop() > 0) {
                        mFrameLayout.setTranslationY(view.getTop() - mFrameLayout.getMeasuredHeight());
                    }
                    else {
                        mFrameLayout.setTranslationY(0f);
//                        headerText.setText(view.getContentDescription());
                    }
                } else {
                    mFrameLayout.setTranslationY(0f);
                }
            }
        });
        mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {
                if (!hasScroll){
                    View view1 = mRecyclerView.findChildViewUnder(mFrameLayout.getMeasuredWidth()/2, mFrameLayout.getMeasuredHeight());
                    if(view1!=null && ((int)view1.getTag(R.id.tag_itemView)) == 1)
                        headerText.setText(view1.getContentDescription());
                }
            }
            @Override
            public void onChildViewDetachedFromWindow(View view) {

            }
        });
    }

    public void setLayoutManager(RecyclerView.LayoutManager manager) {
        mRecyclerView.setLayoutManager(manager);
    }

    public void addItemDecoration(RecyclerView.ItemDecoration decoration) {
        if (decoration != null) {
            mRecyclerView.addItemDecoration(decoration);
        }
    }

    public void setAdapter(MultiItemAdapter adapter) {
        this.adapter = adapter;
        mRecyclerView.setAdapter(adapter);
    }

    public void setHeaderLayout(int resId) {
        View view = LayoutInflater.from(getContext()).inflate(resId,mFrameLayout,true);
        headerText = (TextView) view.findViewById(R.id.item_tv);
        headerText.setText("A");
    }

}
